package com.leve.control.service;

import com.alibaba.fastjson.JSON;
import com.leve.control.constant.ErrorCode;
import com.leve.control.domain.Cuser;
import com.leve.control.domain.SmsSendRequest;
import com.leve.control.domain.SmsSendResponse;
import com.leve.control.mapper.CuserMapper;
import com.leve.control.util.*;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author whongyu
 * @create by 2019/6/12
 */
@Service
@Slf4j
public class CuserService {

    // 用户平台API账号
    private static String account = "N531435_N3104345";
    // 用户平台API密码
    private static String password = "7xRfdZMeBg338a";

    @Resource
    private CuserMapper cuserMapper;
    @Autowired
    private RedisUtil redisUtil;

    public Result login(String mobile ,String verificationCode){
        Map<String ,Object> map = new HashMap<>();
        map.put("mobile",mobile);
        List<Cuser> users = cuserMapper.findUser(map);
        Cuser user = null;
        if (users.size()<=0 || users == null){
            user = new Cuser();
            user.setCuserId(UuidUtil.getUUID());
            user.setMobile(mobile);
            user.setCreateTime(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
            cuserMapper.add(user);
        }
        String code = redisUtil.gets(mobile);
        if(code.equals(verificationCode)){
            if(users.size()>0 && users !=null){
                user = users.get(0);
                return new Result(ErrorCode.SUCCESS,user);
            }else {
                return new Result(ErrorCode.SUCCESS,user);
            }
        }else {
            return new Result(ErrorCode.E_20011);
        }
    }

    /**
     * 手机验证码 发送
     * @param moblie
     * @return
     */
    public int sendSms(String moblie){
        //请求地址请登录253云通讯自助通平台查看
        String smsSingleRequestServerUrl = "http://smsbj1.253.com/msg/send/json";
        // 短信内容
        int a = (int)((Math.random()*9+1)*100000);
        redisUtil.set(moblie,a,5L, TimeUnit.MINUTES);//5分钟失效
        String msg = "【力维共享植保】你好,你的验证码是:"+a+" ,5分钟内有效，请勿泄露于他人!";
        //状态报告
        String report= "true";

        SmsSendRequest smsSingleRequest = new SmsSendRequest(account, password, msg, moblie,report);

        String requestJson = JSON.toJSONString(smsSingleRequest);

        log.info("手机验证码发送成功!手机号"+moblie+":验证码为："+ a);

        String response = HttpUtils.sendSmsByPost(smsSingleRequestServerUrl, requestJson);

        SmsSendResponse smsSingleResponse = JSON.parseObject(response, SmsSendResponse.class);

        System.out.println("response  toString is :" + smsSingleResponse);

        return a;
    }


    public List<Cuser> findCuser(Cuser cuser){
        Map<String,Object> map = new HashMap<>();
        map.put("mobile",cuser.getMobile());
        map.put("name",cuser.getName());
        return cuserMapper.findUser(map);
    }

    public int removeCuser(String cuserId){
        return cuserMapper.remove(cuserId);
    }

    public int addCuser(Cuser cuser){
        return cuserMapper.add(cuser);
    }

    public Cuser findByMobile(String mobile){
        return cuserMapper.findByMobile(mobile);
    }
}
